#include "win32Container.h"
win32Container* win32Container::root = nullptr;
LRESULT CALLBACK WndProc(HWND hWnd,
	UINT msg,
	WPARAM wParam,
	LPARAM lParam) 
{
	return win32Container::GetInterface()->mWndProc(hWnd,msg,wParam,lParam);
}
win32Container::win32Container(
	HINSTANCE hInstance, 
	const wstring WndClassName,
	int ShowWnd, 
	int width, 
	int height, 
	bool windowed):
	hInstance(hInstance),
	WndClassName(WndClassName),
	Width(width),
	Height(height)
{
	root = this;
	WNDCLASSEX wc = { };
	wc.cbSize = sizeof(WNDCLASSEX);    //Size of our windows class
	wc.style = CS_HREDRAW | CS_VREDRAW;    //class styles
	wc.lpfnWndProc = WndProc;    //Default windows procedure function
	wc.cbClsExtra = NULL;    //Extra bytes after our wc structure
	wc.cbWndExtra = NULL;    //Extra bytes after our windows instance
	wc.hInstance = hInstance;    //Instance to current application
	wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);    //Title bar Icon
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);    //Default mouse Icon
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);    //Window bg color
	wc.lpszMenuName = NULL;    //Name of the menu attached to our window
	wc.lpszClassName = WndClassName.c_str();    //Name of our windows class

	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, L"Error registering class",
			L"Error", MB_OK | MB_ICONERROR);
		initialized = false;
	}
	hwnd = CreateWindowEx(
		NULL, WndClassName.c_str(), L"Default Title",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
		width, height, NULL, NULL, hInstance, NULL
	);
	if (!hwnd)
	{
		MessageBox(NULL, L"Error creating window",
			L"Error", MB_OK | MB_ICONERROR);
		initialized = false;
	}
	ShowWindow(hwnd, ShowWnd);
	UpdateWindow(hwnd);
}

LRESULT win32Container::mWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_ACTIVATE:
		if (LOWORD(wParam) == WA_INACTIVE)
		{
			mAppPaused = true;
		}
		else
		{
			mAppPaused = false;
		}
		return 0;
	case WM_LBUTTONDOWN: case WM_MBUTTONDOWN: case WM_RBUTTONDOWN:
	case WM_LBUTTONUP: case WM_MBUTTONUP: case WM_RBUTTONUP:
		mAppPaused = false;
		return 0;
	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE) {
			DestroyWindow(hWnd);
		}
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

win32Container * win32Container::GetInterface()
{
	return root;
}
